/**
 * @Description:
 * @author 紫霞
 * @since 2019/1/17
 */
// React基础组件
import React from 'react'
// dva 连接组件 - 连接route和model
import { connect } from 'dva'
import FlagBtn from 'widget/FlagBtn/FlagView'
// 引入antd的组件
import { Row, Col, Card, Button, Tabs, Form, Popconfirm, Modal, } from 'antd'
//导入路由组件，用于跳转等
import { Link, hashHistory } from 'dva/router'
// 导入项目全局配置文件
import config from 'config/config'
import Cookie from 'js-cookie'
// 引入model取值工具方法
import { mapStateToProps } from 'utils/view_utils'
//引用listPage
import ListPage from 'widget/PageTmpl/ListPage'
import cx from 'classnames'
// 当前组件样式
import styles from './integralManagementStyle.less'
import { Grid, QueryCondition } from 'widget/Grid/index'
import { AdvanceSearch } from 'widget/CollapsiblePanels/index'
import Keypress  from 'widget/Keypress'
import { chunk } from 'lodash'

/**
 * description 路由视图及事件处理
 * dispatch 分发到models中的effects对应事件名
 * model 用于获取models中state定义的数据
 * form 表单对象
 */
const TabPane = Tabs.TabPane
const confirm = Modal.confirm;

const routeView = function ({ dispatch, model, form }) {

  const { resetFields, getFieldDecorator, validateFields, setFieldsValue } = form
  const { modelObj, namespace } = model
  let {regionList } = modelObj

  function detailSkip(type, id = '') {
    hashHistory.push({
      pathname: '/home/integralMall/integralManagementDetail',
      query: { pageType:type, id }
    });
  }
  // function showDeleteConfirm(id) {
  //   console.log(id)
  //   confirm({
  //     title: '您确定删除吗?',
  //     content: '删除后不可恢复',
  //     okText: '确定',
  //     okType: 'danger',
  //     cancelText: '取消',
  //     onOk() {

  //       dispatch({
  //         type: `${namespace}/delPremise`,
  //         payload: { productId: id }
  //       });
  //     },
  //     onCancel() {
  //       console.log('Cancel');
  //     },
  //   });
  // }
  let pageProps = {
    ui: {
      "api_url": "jfsc/admin/gift/list",
      "method": "POST",
      "app": "",
      "params": {uid:localStorage.getItem("username"),regionId:Cookie.get(config.cookie.regionId)},
      // "mockData": mockData,
      "mockData": null,
      search_bar: {
        fields: [
          {
            en_name: 'issj',
            zh_name: '是否上架',
            elem_type: 'Select',
            elem_valid_type: 'string',
            cmpt_items: [
              {
                value: 'Y',
                label: '是'
              },
              {
                value: 'N',
                label: '否'
              }
            ]
          },
          {
            en_name: 'isPost',
            zh_name: '是否邮寄',
            elem_type: 'Select',
            elem_valid_type: 'string',
            cmpt_items: [
              {
                value: 'Y',
                label: '邮寄'
              },
              {
                value: 'N',
                label: '自取'
              }
            ]
          },
          {
            en_name: 'giftName',
            zh_name: '商品名称',
            elem_type: 'Input',
            elem_valid_type: 'string',
          },
          {
            en_name: 'regionId',
            zh_name: '区域名称',
            elem_type: 'Select',
            elem_valid_type: 'string',
            cmpt_items: regionList
          },
        ],
        searchHandler: () => { // 自定义搜索按钮回调函数，点击搜索时触发
          console.log('search click')
          //dispatch({ type: `${namespace}/updateStore`, payload: { queryList: [1, 2, 3, 4] } })
          // dispatch({ type: `${namespace}/updateStore`, payload: { queryList: modelObj.biz.queryForm } })
        },
        resetHandler: () => { // 自定义重置按钮回调函数，点击重置时触发
          console.log('reset click')
        },
        searchCallback: params => { // 自定义搜索完成后的后调函数, 搜索完成后触发, 并将本次搜索的参数传入
          console.log('search finish', params)
        },
      },

      action_bar: [
        {
          label:'新建商品',
          type:'primary',
          btnId:'',
          onClick:(e,row)=>{
            detailSkip("add")
          }
        },
        // {
        //   label:'导出商品信息',
        //   type:'primary',
        //   btnId:'',
        //   onClick: (e, row) => {
        //     dispatch({
        //       type: `${namespace}/exportFeedback`,
        //       payload: {}
        //     })
        //   }
        // }
      ],
      // 数据表格
      "table":
          {
            "status_params": "status=0",
            "status_text": "全部",
            "scroll": { x: 1800 },
            "fields": [
              {
                en_name: "key",
                zh_name: "序号",
                width: 80,
                render: (a, item, index, d, e, f) => {
                  return index + 1
                },
              },
              {
                "en_name": "issj",
                "zh_name": "是否上架",
                render: (text, record) => {
                  return <div>
                    {{ 'Y': '是', 'N': '否' }[text] || ''}
                  </div>
                }
              },
              {
                "en_name": "isPost",
                "zh_name": "是否邮寄",
                render: (text, record) => {
                  return <div>
                    {{ 'Y': '邮寄', 'N': '自取' }[text] || ''}
                  </div>
                }
              },
              {
                "en_name": "giftName",
                "zh_name": "商品名称",
              },
              {
                "en_name": "giftJj",
                "zh_name": "商品简介",
                width: 280
              },
              {
                "en_name": "jfMerchandiseName",
                "zh_name": "商品类别",
              },
              {
                "en_name": "jfMerchantName",
                "zh_name": "商家名称",
              },
              {
                "en_name": "isUnion",
                "zh_name": "是否商家联盟",
                render: (text, row, index) => {
                    if(text == 1){
                        return '是'
                    }else{
                        return '否'
                    }
                },
              },
              {
                "en_name": "integral",
                "zh_name": "兑换积分",
              },
              {
                "en_name": "imgUrl",
                "zh_name": "图片",
                width:160,
                render:(text,item,index)=>{
                  if(text){
                    if(text.indexOf(",") != -1){
                      text = text.substring(0,text.indexOf(","));
                    }
                    return <img style={{width:'80px',height:'50px'}} src={text}></img>
                  }
                }
              },
              {
                "en_name": "sumLimit",
                "zh_name": "兑换数量限制",
              },
              {
                "en_name": "stock",
                "zh_name": "库存数量",
              },
              {
                "en_name": "remark",
                "zh_name": "备注",
              },
              {
                "en_name": "regionName",
                "zh_name": "区域",
              },
              {
                "en_name": "edit",
                "zh_name": "操作",
                fixed:'right',
                width:120,
                render: (a, item, index, d, e, f) => {
                  return (
                    <span className={styles.pot}>
                      {/* <FlagBtn node={ <a onClick={() => showDeleteConfirm(item.productId)}>删除<span className="ant-divider" /></a>}
                      btnId={'130101'}
                      /> */}
                      <FlagBtn node={ <a onClick={() => detailSkip('edit', item.id)}>编辑<span className="ant-divider" /></a>}
                      btnId={''}
                      />
                      <FlagBtn node={ <a onClick={() => detailSkip('detail', item.id)}>查看</a>}
                      btnId={''}
                      />
                    </span>
                  )
                },

              },
            ],
            // 表格操作
          },

    },
  }
  if (!!modelObj.biz) {
    pageProps.biz = modelObj.biz
    pageProps.biz.syncBackCbf = (biz) => {
      console.log(biz)
      dispatch({
        type: `${namespace}/updateStore`,
        payload: { biz }
      })
    }
  }
  return (
      <div className={cx('pageLayout')}>
        <div className={styles.table}>
          <ListPage pageProps={pageProps} />
        </div>
      </div>
  )
}

// 连接视图(view)和模型(model)
export default connect(mapStateToProps)(Form.create()(routeView))
